<?php

namespace App\Http\Controllers\Admin\GameTool;

use App\Services\GameTool\RedPacketService;
use App\Http\Requests\Admin\GameTool\RedPacketIndexRequest;
use App\Http\Requests\Admin\GameTool\RedPacketWithdrawRequest;
use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;

class RedPacketController extends BaseController
{

    public function __construct(RedPacketService $redPacketService)
    {
        $this->redPacketService = $redPacketService;
    }

    //红包管理 列表页
    public function index(RedPacketIndexRequest $request)
    {
        $reqStartTime = $request->start_time;
        $reqEndTime = $request->end_time;
        $reqPageSize = $request->page_size;
        $reqOrderBy = $request->order_by;
        $reqOrderDirection = $request->order_direction;

        $data = $this->redPacketService->getAllRedPacket($request);

        return $this->view(null, compact(['reqStartTime', 'reqEndTime', 'reqPageSize', 'data', 'reqOrderBy', 'reqOrderDirection']));
    }

    //同意提现
    public function withdraw(RedPacketWithdrawRequest $request)
    {
        list($code, $msg) = $this->redPacketService->agreeWithdraw($request);

        if ($code == 1) {
            flash('ID:' . $request['id'] . '提现成功')->success()->important();
        } else if ($code == 4) {
            flash('ID:' . $request['id'] . ',支付宝打款失败,' . $msg)->error()->important();
        } else {
            flash('ID:' . $request['id'] . '提现失败')->error()->important();
        }

        return redirect()->back();
    }

    //拒绝提现
    public function reject(RedPacketWithdrawRequest $request)
    {
        list($code, $msg) = $this->redPacketService->rejectWithdraw($request);

        if ($code == 1) {
            flash('ID:' . $request['id'] . '拒绝提现,操作成功')->success()->important();
        } else {
            flash('ID:' . $request['id'] . '拒绝提现,操作失败,' . $msg . ',' . $code)->error()->important();
        }

        return redirect()->back();
    }
}
